package cn.usian.service.impl;

import cn.usian.dao.UserMapper;
import cn.usian.pojo.User;
import cn.usian.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

   /* @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }*/

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public void del(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public Page<User> findPage(Integer pageIndex, Map searchMap) {
        Page<User> page = userMapper.selectPage(new Page<User>(1, 2), null);
        long current = page.getCurrent();
         List<User> records = page.getRecords();

        LambdaQueryWrapper<User>queryWrapper=new LambdaQueryWrapper();
        System.out.println(searchMap.get("name"));
        queryWrapper.like(searchMap.get("name")!=null,User::getName,searchMap.get("name"));

        queryWrapper.gt(searchMap.get("minAge")!="" && searchMap.get("minAge")!=null,User::getAge,searchMap.get("minAge"));

        queryWrapper.lt(searchMap.get("maxAge")!="" && searchMap.get("maxAge")!=null,User::getAge,searchMap.get("maxAge"));

        return userMapper.selectPage(new Page<User>(pageIndex,2),queryWrapper);

    }
}
